<!DOCTYPE html>
<html lang="">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta itemprop="name" content="Mysql安装">
<meta itemprop="description" content="mysql安装  在./docker-compose中，增加service，实例如下
 version: &#34;3.7&#34;services:# mysql数据库 mysql:image: mysql:5.7.35container_name: &#34;mysql&#34;environment:# root 密码  - MYSQL_ROOT_PASSWORD=h!AlL608nGLnY3Abe0KL# 时区 - TZ=Asia/Shanghaivolumes:# 数据及配置挂载到容器，在第一次启动容器后，复制出来，然后重建容器 # - ./mysql/lib/mysql:/var/lib/mysql # - ./mysql/conf.d:/etc/mysql/conf.d logging:driver: json-fileoptions:max-size: &#34;10M&#34;max-file: &#34;10&#34;restart: always数据及配置挂载  复制容器文件到主机  cd mysql;# 复制数据docker cp mysql:/var/lib/mysql ./lib# 复制配置docker cp mysql:/etc/mysql/conf.d ./# 修改docker-compose volumesvolumes:- .">
<meta itemprop="datePublished" content="2023-06-27T11:04:55&#43;08:00" />
<meta itemprop="dateModified" content="2023-06-27T11:04:55&#43;08:00" />
<meta itemprop="wordCount" content="89">



<meta itemprop="keywords" content="" /><meta property="og:title" content="Mysql安装" />
<meta property="og:description" content="mysql安装  在./docker-compose中，增加service，实例如下
 version: &#34;3.7&#34;services:# mysql数据库 mysql:image: mysql:5.7.35container_name: &#34;mysql&#34;environment:# root 密码  - MYSQL_ROOT_PASSWORD=h!AlL608nGLnY3Abe0KL# 时区 - TZ=Asia/Shanghaivolumes:# 数据及配置挂载到容器，在第一次启动容器后，复制出来，然后重建容器 # - ./mysql/lib/mysql:/var/lib/mysql # - ./mysql/conf.d:/etc/mysql/conf.d logging:driver: json-fileoptions:max-size: &#34;10M&#34;max-file: &#34;10&#34;restart: always数据及配置挂载  复制容器文件到主机  cd mysql;# 复制数据docker cp mysql:/var/lib/mysql ./lib# 复制配置docker cp mysql:/etc/mysql/conf.d ./# 修改docker-compose volumesvolumes:- ." />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://www.lican.site/postsbak/mysql%E5%AE%89%E8%A3%85/" />
<meta property="article:published_time" content="2023-06-27T11:04:55+08:00" />
<meta property="article:modified_time" content="2023-06-27T11:04:55+08:00" />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Mysql安装"/>
<meta name="twitter:description" content="mysql安装  在./docker-compose中，增加service，实例如下
 version: &#34;3.7&#34;services:# mysql数据库 mysql:image: mysql:5.7.35container_name: &#34;mysql&#34;environment:# root 密码  - MYSQL_ROOT_PASSWORD=h!AlL608nGLnY3Abe0KL# 时区 - TZ=Asia/Shanghaivolumes:# 数据及配置挂载到容器，在第一次启动容器后，复制出来，然后重建容器 # - ./mysql/lib/mysql:/var/lib/mysql # - ./mysql/conf.d:/etc/mysql/conf.d logging:driver: json-fileoptions:max-size: &#34;10M&#34;max-file: &#34;10&#34;restart: always数据及配置挂载  复制容器文件到主机  cd mysql;# 复制数据docker cp mysql:/var/lib/mysql ./lib# 复制配置docker cp mysql:/etc/mysql/conf.d ./# 修改docker-compose volumesvolumes:- ."/>

	<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
	<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
	<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
	<link rel="manifest" href="/site.webmanifest">
	<link rel="mask-icon" href="/safari-pinned-tab.svg" color="">
	<link rel="shortcut icon" href="/favicon.ico">

	<title>Mysql安装</title>
	<link rel="stylesheet" href="https://www.lican.site/css/style.min.d3141168199607bf3a517216ce3c263814eecdbc8fca72a9a88700799a838219.css">
	
</head>

<body id="page">
	
	<header id="site-header" class="animated slideInUp faster">
		<div class="hdr-wrapper section-inner">
			<div class="hdr-left">
				<div class="site-branding">
					<a href="https://www.lican.site">花落雨忧</a>
				</div>
				<nav class="site-nav hide-in-mobile">
					<a href="https://www.lican.site/posts">首页</a>
					<a href="https://www.lican.site/tech/">技术文档</a>
					<a href="https://www.lican.site/article/">文章</a>
					<a href="https://www.lican.site/project/">项目</a>
					<a href="https://www.lican.site/about/">关于</a>
				</nav>
			</div>
			<div class="hdr-right hdr-icons">
				<button id="menu-btn" class="hdr-btn" title="Menu"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-menu"><line x1="3" y1="12" x2="21" y2="12"></line><line x1="3" y1="6" x2="21" y2="6"></line><line x1="3" y1="18" x2="21" y2="18"></line></svg></button>
			</div>
		</div>
	</header>
	<div id="mobile-menu" class="animated fast">
		<ul>
			<li><a href="https://www.lican.site/posts">首页</a></li>
			<li><a href="https://www.lican.site/tech/">技术文档</a></li>
			<li><a href="https://www.lican.site/article/">文章</a></li>
			<li><a href="https://www.lican.site/project/">项目</a></li>
			<li><a href="https://www.lican.site/about/">关于</a></li>
		</ul>
	</div>


	<main class="site-main section-inner thin animated fadeIn faster">
		<h1>Mysql安装</h1>
		<div class="content">
			<h3 id="mysql安装">mysql安装<a href="#mysql安装" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h3>
<blockquote>
<p>在./docker-compose中，增加service，实例如下</p>
</blockquote>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-yaml" data-lang="yaml"><span style="color:#66d9ef">version</span>: <span style="color:#e6db74">&#34;3.7&#34;</span>
services:
  <span style="color:#75715e"># mysql数据库</span>
  mysql:
    <span style="color:#66d9ef">image</span>: mysql:<span style="color:#ae81ff">5.7.35</span>
    <span style="color:#66d9ef">container_name</span>: <span style="color:#e6db74">&#34;mysql&#34;</span>
    environment:
      <span style="color:#75715e"># root 密码 </span>
      - MYSQL_ROOT_PASSWORD=h!AlL608nGLnY3Abe0KL
      <span style="color:#75715e"># 时区</span>
      - TZ=Asia/Shanghai
    volumes:
      <span style="color:#75715e"># 数据及配置挂载到容器，在第一次启动容器后，复制出来，然后重建容器</span>
      <span style="color:#75715e"># - ./mysql/lib/mysql:/var/lib/mysql</span>
      <span style="color:#75715e"># - ./mysql/conf.d:/etc/mysql/conf.d</span>
    logging:
      <span style="color:#66d9ef">driver</span>: json-file
      options:
        <span style="color:#66d9ef">max-size</span>: <span style="color:#e6db74">&#34;10M&#34;</span>
        <span style="color:#66d9ef">max-file</span>: <span style="color:#e6db74">&#34;10&#34;</span>
    <span style="color:#66d9ef">restart</span>: always
</code></pre></div><h1 id="数据及配置挂载">数据及配置挂载<a href="#数据及配置挂载" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h1>
<ul>
<li>复制容器文件到主机</li>
</ul>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">cd</span> <span style="color:#a6e22e">mysql</span>;
<span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">复制数据</span>
<span style="color:#a6e22e">docker</span> <span style="color:#a6e22e">cp</span> <span style="color:#a6e22e">mysql</span>:<span style="color:#f92672">/</span><span style="color:#66d9ef">var</span><span style="color:#f92672">/</span><span style="color:#a6e22e">lib</span><span style="color:#f92672">/</span><span style="color:#a6e22e">mysql</span> .<span style="color:#f92672">/</span><span style="color:#a6e22e">lib</span>
<span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">复制配置</span>
<span style="color:#a6e22e">docker</span> <span style="color:#a6e22e">cp</span> <span style="color:#a6e22e">mysql</span>:<span style="color:#f92672">/</span><span style="color:#a6e22e">etc</span><span style="color:#f92672">/</span><span style="color:#a6e22e">mysql</span><span style="color:#f92672">/</span><span style="color:#a6e22e">conf</span>.<span style="color:#a6e22e">d</span> .<span style="color:#f92672">/</span>
<span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">修改docker</span><span style="color:#f92672">-</span><span style="color:#a6e22e">compose</span> <span style="color:#a6e22e">volumes</span>
<span style="color:#a6e22e">volumes</span>:
  <span style="color:#f92672">-</span> .<span style="color:#f92672">/</span><span style="color:#a6e22e">mysql</span><span style="color:#f92672">/</span><span style="color:#a6e22e">lib</span><span style="color:#f92672">/</span><span style="color:#a6e22e">mysql</span>:<span style="color:#f92672">/</span><span style="color:#66d9ef">var</span><span style="color:#f92672">/</span><span style="color:#a6e22e">lib</span><span style="color:#f92672">/</span><span style="color:#a6e22e">mysql</span>
  <span style="color:#f92672">-</span> .<span style="color:#f92672">/</span><span style="color:#a6e22e">mysql</span><span style="color:#f92672">/</span><span style="color:#a6e22e">conf</span>.<span style="color:#a6e22e">d</span>:<span style="color:#f92672">/</span><span style="color:#a6e22e">etc</span><span style="color:#f92672">/</span><span style="color:#a6e22e">mysql</span><span style="color:#f92672">/</span><span style="color:#a6e22e">conf</span>.<span style="color:#a6e22e">d</span>
</code></pre></div><h1 id="mysql配置">mysql配置<a href="#mysql配置" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h1>
<blockquote>
<p>需要修改./mysql/conf.d/my.cnf</p>
</blockquote>
<ul>
<li>因为mysql5.7以后，mysql的sql_mode默认配置为ONLY_FULL_GROUP_BY,该值表示：对于使用 GROUP BY 进行查询的SQL，不允许 SELECT 部分出现 GROUP BY 中未出现的字段</li>
</ul>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go">[<span style="color:#a6e22e">mysqld</span>]
<span style="color:#a6e22e">sql_mode</span>=<span style="color:#a6e22e">STRICT_TRANS_TABLES</span>,<span style="color:#a6e22e">ERROR_FOR_DIVISION_BY_ZERO</span>,<span style="color:#a6e22e">NO_AUTO_CREATE_USER</span>,<span style="color:#a6e22e">NO_ENGINE_SUBSTITUTION</span>
</code></pre></div><blockquote>
<p>解释</p>
</blockquote>
<ul>
<li>STRICT_TRANS_TABLES,选项针对事务性存储引擎生效，对于非事务性存储引擎无效，该选项表示开启strict sql模式</li>
<li>ERROR_FOR_DIVISION_BY_ZERO,INSERT或者UPDATE语句中，如果数据被0除，则出现警告（非strict sql模式下）或者错误(strict sql模式下)</li>
<li>NO_AUTO_CREATE_USER,之前版本中使用GRANT语法，如果用户不存在则会自动创建用户，该选项限制该功能</li>
<li>NO_ENGINE_SUBSTITUTION,在使用CREATE TABLE或者ALTER TABLE语法执行存储引擎的时候，如果设定的存储引擎被禁用或者未编译，会产生错误。</li>
</ul>
<blockquote>
<p>严格模式下，mysql自身会对数据进行严格的校验（格式、长度、类型等），比如一个整型字段我们写入一个字符串类型的数据，在非严格模式下MySQL不会报错，同样如果定义了char或varchar类型的字段，当写入或更新的数据超过了定义的长度也不会报错。我认为这个对于编程来说没有任何好处，虽然我们尽量在代码中做数据校验。MySQL开启了严格模式从一定程序上来讲师对我们代码的一种测试，如果我们的开发环境没有开启严格模式在开发过程中也没有遇到错误，那么在上线或代码移植的时候将有可能出现不兼容的情况，因此在开发过程做最好开启MySQL的严格模式。</p>
</blockquote>

		</div>
	</main>

	<footer id="site-footer" class="section-inner thin animated fadeIn faster">
		<p>&copy; 2024 <a href="https://www.lican.site"></a></p>
		<p>
			Made with <a href="https://gohugo.io/" target="_blank" rel="noopener">Hugo</a> &#183; Theme <a href="https://github.com/Track3/hermit" target="_blank" rel="noopener">Hermit</a> &#183; <a href="https://www.lican.site/post/index.xml" target="_blank" title="rss"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-rss"><path d="M4 11a9 9 0 0 1 9 9"></path><path d="M4 4a16 16 0 0 1 16 16"></path><circle cx="5" cy="19" r="1"></circle></svg></a>
		</p>
	</footer>


	<script src="https://www.lican.site/js/main.min.784417f5847151f848c339cf0acb13a06cbb648b1483435a28ed4556c4ead69b.js"></script>

</body>

</html>
